package com.bxait.toymail.product.service.impl;

import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.com.vo.product.AttrGroupRelationVo;
import com.bxait.toymail.product.dao.AttrAttrgroupRelationDao;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.product.entity.AttrAttrgroupRelationEntity;
import com.bxait.toymail.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Resource
    AttrAttrgroupRelationDao relationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /***
     * @param vos
     * @==============History===============<br/>
     * @Description // TODO  添加属性与分组关联关系
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.vo.product.AttrGroupRelationVo[]]
     */
    @Transactional
    @Override
    public void addRelation(AttrGroupRelationVo[] vos) {

        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(item.getAttrId());
            relationEntity.setAttrGroupId(item.getAttrGroupId());
            relationEntity.setAttrSort(0);
            return relationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

    /**
     * @param vos
     * @==============History===============<br/>
     * @Description // TODO  删除属性属性分组关联关系
     * @Date 2023/3/18 2023/3/18 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.vo.product.AttrGroupRelationVo[]]
     */
    @Transactional
    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
        // 1.准备需要删除的属性与属性关联的实体类
        List<AttrAttrgroupRelationEntity> entities = new ArrayList<>();
        Arrays.asList(vos).stream().forEach(item->{
            AttrAttrgroupRelationEntity relationEntity =
                        new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(item.getAttrId());
            relationEntity.setAttrGroupId(item.getAttrGroupId());
            relationEntity.setAttrSort(0);
            entities.add(relationEntity);
        });
        // 2.删除属性与属性分组的关联关系
        relationDao.deleteRelation(entities);
    }

}